﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Media;

namespace ATM
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Form1 : Form
    {
        Module module = new Module();
        /// <summary>
        /// 
        /// </summary>
        public Form1()
        {
            
            InitializeComponent();
            cardnumberBox.Focus();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void loginButton_Click(object sender, EventArgs e)
        {   double seged;
        if (Double.TryParse(cardnumberBox.Text, out seged) && (cardnumberBox.TextLength == 16))
        {
            if (module.login(this.cardnumberBox.Text, this.pincodeBox.Text))
            {
                toolStripDropDownButton1.Enabled = true;
                takeButton.Visible = true;
                balanceButton.Visible = true;
                logoutButton.Visible = true;
                this.takeButton.Enabled = true;
                this.balanceButton.Enabled = true;
                this.logoutButton.Enabled = true;
                exitButton.Visible = false;
                this.exitButton.Enabled = false;
                this.groupBox1.Enabled = false;
                this.Refresh();
                this.takeButton.Focus();
                SystemSounds.Exclamation.Play();
                toolStripStatusLabel1.Text = "Login Confirmed!";
            }
            else
            {
                MessageBox.Show("Incorrect Password or Card Number! Pleas try again!", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        else
        {
            MessageBox.Show("Incorrect Card Number! Pleas try again!", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exitButton_Click(object sender, EventArgs e)
        {
            GoodbyeForm goodbye = new GoodbyeForm();
            goodbye.Show();
            Thread.Sleep(1500);
            Application.Exit();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void takeButton_Click(object sender, EventArgs e)
        {
            Form2 takeForm = new Form2();
        
                if (takeForm.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        if (module.TakeCash(Convert.ToInt32(takeForm.CashBox.Text)))
                        {
                            MessageBox.Show("The operation was successful!\nRemaining balance: "+module.getBalance()+"$.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        }
                        else
                        {
                            MessageBox.Show("Take cash Rejected! You have not enough money!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Incorrect Amount! Please try again!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.takeButton_Click(sender, e);
                    }
                    
                }
                takeForm.Dispose();
            
        }

       
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void balanceButton_Click(object sender, EventArgs e)
        {
            BalanceForm balance = new BalanceForm();
            
            balance.Set_Balance(module.getBalance());
            balance.ShowDialog();
            balance.Dispose();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void logoutButton_Click(object sender, EventArgs e)  
        {
            module.logout();
            toolStripDropDownButton1.Enabled = false;
            takeButton.Visible = false;
            balanceButton.Visible = false;
            logoutButton.Visible = false;
            this.takeButton.Enabled = false;
            this.balanceButton.Enabled = false;
            this.logoutButton.Enabled = false;
            Thread.Sleep(1000);
            exitButton.Visible = true;
            this.exitButton.Enabled = true;
            this.groupBox1.Enabled = true;
            pincodeBox.Text = "";
            this.Refresh();
            cardnumberBox.Focus();
            toolStripStatusLabel1.Text = "Logout";
        }

        private void loginButton_Click_1(object sender, EventArgs e)
        {
            
        }

        private void pincodeBox_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            if (pincodeBox.TextLength == 4)
            {
                loginButton.Focus();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void changedPINToolStripMenuItem_Click(object sender, EventArgs e)
        {
            changepinForm changePin = new changepinForm();

            if (changePin.ShowDialog() == DialogResult.OK)
            {
                if(!module.ChangePIN(changePin.OldmaskedTextBox1.Text,changePin.NewmaskedTextBox2.Text))
                {
                    MessageBox.Show("Old PIN invalid! Please try again!","Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    changedPINToolStripMenuItem_Click(sender, e);
                }
                else
                {
                    MessageBox.Show("Your PIN code have changed!","Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        

        

       
    }
}
